package net.playcabbage.servlet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import net.playcabbage.beans.CheckinBean2;
import net.playcabbage.utility.CheckinUtility;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

public class GreenServlet extends HttpServlet {

	protected String MIN_X;
	protected String MAX_X;
	protected String MIN_Y;
	protected String MAX_Y;
	protected String MIN_Z;
	protected String MAX_Z;

	private boolean checkinSuccess;
	private String checkinSuccessMessage;

	public void init(ServletConfig servletConfig) throws ServletException{
		this.MIN_X = servletConfig.getInitParameter("min_x");
		this.MAX_X = servletConfig.getInitParameter("max_x");
		this.MIN_Y = servletConfig.getInitParameter("min_y");
		this.MAX_Y = servletConfig.getInitParameter("max_y");
		this.MIN_Z = servletConfig.getInitParameter("min_z");
		this.MAX_Z = servletConfig.getInitParameter("max_z");
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//
//		PrintWriter out = resp.getWriter();
//		out.print("x"+req.getParameter("superid").substring(0,2));
//		out.print("y"+req.getParameter("superid").substring(2,4));
//		out.print("z"+req.getParameter("superid").substring(4,6));
//		
//		
//		CheckinBean checkinBean = null;
//
//		checkinBean = doCheckin(req);
//		if (checkinBean == null) {
//			out.println("null");
//		} else {
//			out.println("NOT null");
//		}
		
		
		
				checkinSuccess = false;
		checkinSuccessMessage = "none";

		HttpSession session = req.getSession();
		if (session.getAttribute("username") == null) {
			session.setAttribute("username","rach");
		}

		CheckinBean2 checkinBean = null;

		checkinBean = doCheckin(req);
		if (checkinBean == null) {
			checkinSuccess = false;
			//checkinSuccessMessage = "SQLException There was a problem checking in! Please try again soon.";
		}

		req.setAttribute("usercookie", req.getSession().getAttribute("username"));
		req.setAttribute("checkinbean", checkinBean);
		req.setAttribute("lastcheckins", getLatestCheckins());


		req.setAttribute("checkinsuccess", checkinSuccess);
		req.setAttribute("checkinsuccessmessage", checkinSuccessMessage);

		RequestDispatcher requestDispatcher =  req.getRequestDispatcher("/templates/home.vm");
		requestDispatcher.forward(req, resp);

	}

	private CheckinBean2 doCheckin(HttpServletRequest req) {
		CheckinBean2 cb = null;
		if (req.getParameter("superid") != null) {
			CheckinUtility cu = new CheckinUtility();
			cb = cu.doCheckin(req.getParameter("superid"));
			if(cb != null) {
				checkinSuccess = true;
				//checkinSuccessMessage = "Checkin was successful";
			} else {
				checkinSuccess = false;
				//checkinSuccessMessage = "There was a problem checking in! Please try again soon.";
			}
		}
		return cb;

	}

	private List<CheckinBean2> getLatestCheckins() {
		CheckinUtility cu = new CheckinUtility();
		List<CheckinBean2> cbList = cu.getLatestCheckins();
		return cbList;

	}


}
